package com.lynn.common.dialog

import android.content.Context
import android.view.View
import android.widget.TextView
import com.lynn.common.utils.SSBUtil
import com.lynn.common.utils.ScreenUtil

/**
 * @description: 隐私声明对话框
 * @author: bailing
 * @datetime: 2025/5/29 11:13
 **/
 class PrivacyDialog {

    class Builder(context: Context) : BaseDialog.Builder(context, R.layout.dialog_privacy) {
        private var mOnClickListener: OnClickListener? = null

        private val ssbUtil by lazy {
            val content = context.getString(R.string.dialog_privacy_content)
            val color = android.R.color.holo_blue_dark
            SSBUtil.Builder(context)
                .setSSB(content)
                .setForColorSpan(color, 44, 53)
                .setForColorSpan(color, 54, 63)
                .setClickSpan(false, color, 44, 53) {
                    mOnClickListener?.onClickPrivacy()
                }
                .setClickSpan(false, color, 54, 63) {
                    mOnClickListener?.onClickUserProtocol()
                }
                .build()

        }

        // Override the convertView method to set the width, horizontal gap, cancelable and canceled on touch outside properties of the dialog
        override fun convertView(viewHolder: ViewHolder, dialog: BaseDialog) {
            // Set the width of the dialog to the screen width of the context
            setWidth(ScreenUtil.getScreenWidth(context))
            // Set the horizontal gap of the dialog to 50dp
            setHorizontalGap(ScreenUtil.dp2px(context, 50f).toInt())

            // Set the dialog to be cancelable
            setCancelable(false)
            // Set the dialog to be canceled on touch outside
            setCanceledOnTouchOutside(false)

            val tvContent = viewHolder.getView<TextView>(R.id.tv_content)
            ssbUtil.updateTextView(tvContent)
            ssbUtil.setLinkMovement(tvContent)


            // Set an OnClickListener for the cancel TextView in the dialog
            viewHolder.setOnClickListener(R.id.tv_cancel, object : View.OnClickListener {
                override fun onClick(v: View?) {
                    // Call the onCancel method of the OnClickListener
                    mOnClickListener?.onCancel()
                    // Dismiss the dialog
                    dialog.dismiss()
                }
            })
            // Set an OnClickListener for the sure TextView in the dialog
            viewHolder.setOnClickListener(R.id.tv_sure) {
                // Call the onSure method of the OnClickListener
                mOnClickListener?.onSure()
                // Dismiss the dialog
                dialog.dismiss()
            }
        }


        fun setOnClickListener(listener: OnClickListener): Builder {
            mOnClickListener = listener
            return this
        }
    }

    interface OnClickListener {
        fun onClickPrivacy()
        fun onClickUserProtocol()
        fun onSure()
        fun onCancel()
    }
}
